package com.xiaoying.server.publish.business.handleresult;

import org.springframework.context.ApplicationContext;

import com.xiaoying.server.publish.entity.UploadResult;
import com.xiaoying.server.publish.service.UploadResultService;
import com.xiaoying.server.publish.utils.RedisUtil;
import com.xiaoying.server.publish.utils.SerializeUtil;
import com.xiaoying.server.publish.utils.SpringUtil;

import redis.clients.jedis.Jedis;


public class SaveUploadResult implements Runnable {

	private ApplicationContext ctx;
	
	public SaveUploadResult(){
		ctx = SpringUtil.getInstance();
	}
	
	@Override
	public void run() {
		try {
//			Thread.sleep(10000);
			this.saveResult();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private void saveResult() throws InterruptedException{		
		UploadResultService uploadResultService = (UploadResultService)ctx.getBean("uploadResultService");
		Jedis redis = RedisUtil.getNewRedisClient();
		while(true){
			if(redis.llen(RedisUtil.KEY_XIAOYING_UPLOAD_RESULT_QUEUE)>0){
				UploadResult uploadResult = (UploadResult)SerializeUtil.unserialize(redis.brpop(1, RedisUtil.KEY_XIAOYING_UPLOAD_RESULT_QUEUE).get(1));
				if(uploadResult!=null){
					if(uploadResultService.save(uploadResult)!=null){
						System.out.println(uploadResult.getRemotePath()+"成功入库");
					}else{
						System.out.println(uploadResult.getRemotePath()+"上传结果失败");
					}
				}else{
//					System.out.println("取队列为null");
				}
			}
		}
	}
}
